package com.googlecode.maratische.google.gui;

import java.awt.Window;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class JInputLocaleIndicator extends JLabel {


	private static class MonitoringThread extends Thread {

		private static final long REFRESH_TIMEOUT = 1000L;

		JInputLocaleIndicator parent;
		
		public MonitoringThread(JInputLocaleIndicator parent) {
			setPriority(NORM_PRIORITY);
			setDaemon(true);
			this.parent = parent;
		}

		public void run() {
			while (!interrupted()) {
				synchronized (indicatorLists) {
					Window window = SwingUtilities.getWindowAncestor(parent);
					System.out.println(window.getInputContext().getLocale().getLanguage());
					System.out.println(window.getAccessibleContext().getLocale().getLanguage());
					
/*					for (Map.Entry<Window, List<JLabel>> entry : indicatorLists.entrySet()) {
						Locale locale = entry.getKey().getInputContext().getLocale();
						for (JLabel label : entry.getValue()) {
							label.setText(locale.getLanguage());
							label.setToolTipText(locale.getDisplayLanguage());
						}
					}*/
				}

				try {
					Thread.sleep(REFRESH_TIMEOUT);
				} catch (InterruptedException thrown) {
					break;
				}
			}
		}

	}

	private static Map<Window, List<JLabel>> indicatorLists = new WeakHashMap<Window, List<JLabel>>();


	public JInputLocaleIndicator() {
		MonitoringThread monitoringThread = new MonitoringThread(this);
		monitoringThread.start();
/*		addHierarchyListener(new HierarchyListener() {
			public void hierarchyChanged(HierarchyEvent event) {
				if ((event.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
					Window window = SwingUtilities.getWindowAncestor(JInputLocaleIndicator.this);

					synchronized (indicatorLists) {
						List<JLabel> indicators = indicatorLists.get(window);

						if (isDisplayable()) {
							if (indicators == null) {
								indicatorLists.put(window, indicators = new ArrayList<JLabel>());
							}

							indicators.add(JInputLocaleIndicator.this);
						} else {
							if (indicators != null) {
								indicators.remove(JInputLocaleIndicator.this);
							}
						}
					}
				}
			}
		});*/
	}
	private static final long serialVersionUID = -703352513128491834L;

}